/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

function ListNode(val) {
  this.val = val;
  this.next = null;
}
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function (head, val) {
  if (!head) return head;
  let newHead = new ListNode();
  //newHead.next 指向链表
  newHead.next = head;
  //用一个变量存储
  let tmpNode = newHead;

  while (tmpNode.next) {
    console.log('newHead', newHead.next)
    if (tmpNode.next.val == val) {
      tmpNode.next = tmpNode.next.next;
    } else {
      tmpNode = tmpNode.next;
    }
  }
  return newHead.next
};

removeElements([1, 2, 6, 3, 4, 5, 6], 6)